<?php


namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;
use think\Session;

class WalletFrozen extends Common
{
    public function index()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        $where = [];
        $whereMap['t1.value'] = ['>', 0];
        if ($search_name && $search_type) {
            if(in_array($search_type, ['id', 'phone'])){
                $where['t2.'.$search_type] = $search_name;
            }elseif(in_array($search_type, ['name'])){
                $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
            }else{
                $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
            }
        }

        $min = input('min');
        $max = input('max');

        if ($min && $max) {
            $where['t1.value'] = ['between', [$min, $max]];
        } else if ($min && !$max) {
            $where['t1.value'] = ['egt', $min];
        } else if (!$min && $max) {
            $where['t1.value'] = ['elt', $max];
        }

        $state = input('state');
        if ($state || $state === '0') {
            switch ((int)$state) {
                case 0:
                    $where['t2.is_banned'] = 1;
                    break;
                case 1:
                    $where['t1.value'] = ['<>', 30];
                    break;
                
                default:
                    break;
            }
            $this->assign('state', $state);
        }

        $this->assign('min', $min);
        $this->assign('max', $max);

        $data = Db::name('wallet_frozen')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->where($whereMap)->field('t1.*,t2.name,t2.is_banned')->order('t1.id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function($item){
            $item['lists'] = Db::name('wallet_frozen_records')->where(['user_id'=>$item['user_id'], 'type' => 0])->order('id desc')->select();
            return $item;
        });
        
        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    public function unfrozen()
    {
        $id = input('id');
        
        if (request()->isAjax()) {
            $value = input('value',0);
            $remark = input('remark','');
            $res = Db::name('wallet_frozen')->where('id', $id)->find();
            $data = ['err' => '解冻失败'];
            if(is_numeric($value)){
                $value = round($value, 2);
                if ($res && $value && $value > 0 && $res['value'] >= $value) {
                    Db::name('wallet_frozen')->where('id', $id)->setDec('value', $value);
                    Db::name('wallet_frozen_records')->insert(['user_id'=>$res['user_id'], 'value'=>$value, 'time'=>time(),'type'=>1,'remark'=>$remark]);
                    $data = ['ret' => '解冻成功'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('wallet_frozen')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    
    public function records()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if(in_array($search_type, ['id', 'phone'])){
                $where['t2.'.$search_type] = $search_name;
            }elseif(in_array($search_type, ['name'])){
                $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
            }else{
                $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
            }
        }

        $start = input('start');
        $end = input('end');

        if ($start && $end) {
            $where['t1.time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['t1.time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['t1.time'] = ['elt', strtotime($end)];
        }
        $this->assign('start', $start);
        $this->assign('end', $end);

        $data = Db::name('wallet_frozen_records')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        
        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function everydaywalletfrozen($value=0, $id)
    {
        $result = Db::name('wallet_frozen')->where(['id'=>$id])->update(['everyday_unfrozen' => floatval($value)]);
        if ($result) {
            return ['state' => 1, 'msg' => '设置成功'];
        }
        return ['state' => 0, 'msg' => '操作错误'];
    }
}